import {Component, ViewEncapsulation} from '@angular/core';
import {FormGroup, FormBuilder, Validators, FormControl} from '@angular/forms';
import {UserService} from '../../services/app.user';
import {Router}  from '@angular/router';
import {AuthenticationHelper} from "../../app.authentication";
import {BaThemeSpinner}  from '../../theme/services';
import {Constants} from '../../../app/common/app.constants';
import {EmailValidator} from "../../theme/validators/email.validator";
declare let toastr:any;

@Component({
    selector: 'forgot',
    encapsulation: ViewEncapsulation.None,
    styleUrls: ['./../login/login.scss'],
    templateUrl: './forgot.html',
    providers: [UserService]
})

export class Forgot {
    constants:Constants;
    form:FormGroup;

    constructor(fb:FormBuilder, private userService:UserService, private router:Router, private authentication:AuthenticationHelper, private spinner:BaThemeSpinner) {
        this.constants = new Constants();
        toastr.options = {positionClass: 'toast-top-right',};
        this.form = fb.group({
            'email': ['', Validators.compose([Validators.required, EmailValidator.validate])]
        });
 }

    // Function call when click button forgot password
    onSubmit(values) {
        this.spinner.showProgress();
        let data = {
            email: values.email
        };
        this.userService.userForgot(data).subscribe(
            data =>this.forgotEmailSucces(data),
            error => this.forgotEmailFail(error));
    }

    // function call when forgot email send successfully
    forgotEmailSucces(result) {
        this.router.navigate(['login']);
        this.spinner.hideProgress();
        toastr.success(this.constants.RESETPASSWORD);
    }

    // function call when forgot email failed
    forgotEmailFail(error) {
        this.spinner.hideProgress();
        toastr.error(error.error_message);
    }
}
